package com.jotta.client.setting;

import java.io.File;

import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.os.Environment;
import android.os.StatFs;

import com.jotta.client.communicate.Configuration;
import com.jotta.client.dataaccess.DataAccess;

public class SettingManager {
	public static SettingManager instance;

	public static final String USER_NAME = "jotta_user_name";
	public static final String PASSWORD = "jotta_password";
	private static final String AUTO_LOGIN = "jotta_autologin";
	private static final String LINK_TO_HELP_VIEW = "http://www.jotta.no/support";
	private static final String LINK_TO_FACEBOOK = "http://www.facebook.com/jottabackup";
	private static final boolean AUTO_LOGIN_DEFAULT_VALUE = true;
	private static final String SERVER_CAPACITY = "jotta_capacity";
	private static final String SERVER_USAGE = "jotta_usage";
	private static final String LOCAL_STORAGE_CAPACITY = "local_capacity";
	private static final String LATEST_BROWSE_PATH = "LatestBrowsePath";
	private static final String LATEST_SWIPE_PATH = "LatestSwipePath";
	private static final String LATEST_FILE_VIEW = "LatestFileView";
	
	/**
	 * Skeleton method
	 */
	public static SettingManager getInstance() {
		if (instance == null)
			instance = new SettingManager();
		return instance;
	}

	/**
	 * Save auto login value from setting view into preferences
	 * 
	 * @param value
	 */
	public void setAutoLogin(boolean value) {
		SharedPreferences settings = Configuration.getReferences();
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(AUTO_LOGIN, value);
		// Commit the edits!
		editor.commit();
	}

	/**
	 * Get auto login value If it in SharePreferences: get this value If not in
	 * SharePreferences: use default value
	 * 
	 * @param value
	 */
	public boolean getAutoLogin() {
		SharedPreferences settings = Configuration.getReferences();
		boolean autoLogin = settings.getBoolean(AUTO_LOGIN,
				AUTO_LOGIN_DEFAULT_VALUE);
		return autoLogin;
	}

	/**
	 * Save username into preferences
	 * 
	 * @param username
	 */
	public void setUsername(String username) {
		SharedPreferences settings = Configuration.getReferences();
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(USER_NAME, username);
		// Commit the edits!
		editor.commit();
	}

	/**
	 * Get username from SharePreferences get value
	 * 
	 * @param value
	 */
	public String getUsername() {
		SharedPreferences settings = Configuration.getReferences();
		String username = settings.getString(USER_NAME, "");
		return username;
	}

	/**
	 * Save password into preferences, if you have time or ability try to
	 * encrypt data
	 * 
	 * @param password
	 */
	public void setPassword(String password) {
		SharedPreferences settings = Configuration.getReferences();
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(PASSWORD, password);
		// Commit the edits!
		editor.commit();
	}

	/**
	 * Get password from SharePreferences get value, if you have time or ability
	 * try to decrypt data
	 * 
	 * @param value
	 */
	public String getPassword() {
		SharedPreferences settings = Configuration.getReferences();
		String password = settings.getString(PASSWORD, "");
		return password;
	}

	/**
	 * Get language
	 * 
	 * @param value
	 */
	public String getLanguage() {
		String language = java.util.Locale.getDefault().getDisplayName();
		return language;
	}

	/**
	 * Get server usage
	 * 
	 */
	public long getServerUsage() {
		SharedPreferences settings = Configuration.getReferences();
		long usage = settings.getLong(SERVER_USAGE, 0);
		return usage;
	}

	/**
	 * Set server usage
	 * 
	 */
	public long setServerUsage(long value) {
		SharedPreferences settings = Configuration.getReferences();
		SharedPreferences.Editor editor = settings.edit();
		editor.putLong(SERVER_USAGE, value);
		// Commit the edits!
		editor.commit();
		return 0;
	}

	/**
	 * Set server capacity
	 * 
	 * @param value
	 * @return
	 */
	public long setServerCapacity(long value) {
		SharedPreferences settings = Configuration.getReferences();
		SharedPreferences.Editor editor = settings.edit();
		editor.putLong(SERVER_CAPACITY, value);
		// Commit the edits!
		editor.commit();
		return 0;
	}

	/**
	 * Get capacity of server
	 * 
	 */
	public long getServerCapacity() {
		SharedPreferences settings = Configuration.getReferences();
		long capacity = settings.getLong(SERVER_CAPACITY, 0);
		return capacity;
	}

	/**
	 * Get server free space
	 * 
	 * @return
	 */
	public long getServerFreeSpace() {
		if (getServerCapacity() == -1)
			return -1;
		long freeSpace = getServerCapacity() - getServerUsage();
		if (freeSpace < 0)
			return 0;
		else
			return freeSpace;
	}

	/**
	 * Get local usage
	 * 
	 */
	public long getLocalUsage() {
		if (DataAccess.getInstance().checkSDCardIsMounted()) {
			File dir = DataAccess.getInstance().getHomeFolder();
			if (dir.exists()) {
				return DataAccess.getInstance().getFileSize(dir);
			}
		}
		return 0;
	}

	/**
	 * Set local storage capacity
	 * 
	 * @param value
	 */
	public void setLocalStorageCapacity(long value) {
		SharedPreferences settings = Configuration.getReferences();
		SharedPreferences.Editor editor = settings.edit();
		editor.putLong(LOCAL_STORAGE_CAPACITY, value);
		editor.commit();
	}

	/**
	 * Get local storage capacity
	 * 
	 * @return
	 */
	public long getLocalStorageCapacity() {
		SharedPreferences settings = Configuration.getReferences();
		long settingStorageSize = settings.getLong(LOCAL_STORAGE_CAPACITY,
				getMaxSizeOfStorage());

		if (settingStorageSize < getLocalUsage())
			settingStorageSize = getLocalUsage();
		else if (settingStorageSize > getMaxSizeOfStorage())
			settingStorageSize = getMaxSizeOfStorage();

		return settingStorageSize;
	}

	/**
	 * Max size of storage is determine by min(SDCardFreeSpace, server capacity)
	 * 
	 * @return
	 */
	public long getMaxSizeOfStorage() {
		long SDFreeSpace = 0;

		StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
				.getPath());
		SDFreeSpace = (long) stat.getBlockSize()
				* (long) stat.getAvailableBlocks();

		return SDFreeSpace + getLocalUsage();
	}

	/**
	 * Get local free space
	 * 
	 * @return
	 */
	public long getLocalFreeSpace() {
		return getLocalStorageCapacity() - getLocalUsage();
	}

	/**
	 * Get link to help view
	 * 
	 * @return
	 */
	public String getLinkToHelpView() {
		return LINK_TO_HELP_VIEW;
	}

	/**
	 * Get link to facebook
	 * 
	 * @return
	 */
	public String getLinkToFacebook() {
		return LINK_TO_FACEBOOK;
	}

	/**
	 * Set latest browse path
	 * 
	 * @param pref
	 * @param path
	 */
	public void setLatestSwipePath(SharedPreferences pref, String path) {
		SharedPreferences.Editor editor = pref.edit();
		editor.putString(LATEST_SWIPE_PATH, path);
		editor.commit();
	}

	/**
	 * Get latest browse path
	 * 
	 * @param pref
	 */
	public String getLatestSwipePath(SharedPreferences pref) {
		String path = pref.getString(LATEST_SWIPE_PATH, "");
		return path;
	}
	
	/**
	 * Set latest file view
	 * 
	 * @param pref
	 * @param path
	 */
	public void setLatestFileView(SharedPreferences pref, String fileName) {
		SharedPreferences.Editor editor = pref.edit();
		editor.putString(LATEST_FILE_VIEW, fileName);
		editor.commit();
	}

	/**
	 * Get latest file view
	 * 
	 * @param pref
	 */
	public String getLatestFileView(SharedPreferences pref) {
		String path = pref.getString(LATEST_FILE_VIEW, "");
		return path;
	}


	/**
	 * Set latest browse path
	 * 
	 * @param pref
	 * @param path
	 */
	public void setLatestBrowsePath(String path) {
		SharedPreferences settings = Configuration.getReferences();
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(LATEST_BROWSE_PATH, path);
		editor.commit();
	}

	/**
	 * Get latest browse path
	 * 
	 * @param pref
	 */
	public String getLatestBrowsePath() {
		SharedPreferences settings = Configuration.getReferences();
		String path = settings.getString(LATEST_BROWSE_PATH, "");
		return path;
	}

	/**
	 * Get the version of this application
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public String getVersion(Context context, Class cls) {
		try {
			ComponentName comp = new ComponentName(context, cls);
			PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
			return pinfo.versionName;
		} catch (android.content.pm.PackageManager.NameNotFoundException e) {
			return null;
		}
	}

	/**
	 * Reset user information on setting storage
	 */
	public void resetUserInfo() {
		setAutoLogin(AUTO_LOGIN_DEFAULT_VALUE);
		setUsername("");
		setPassword("");
		setLatestBrowsePath("");
	}

	/**
	 * Set user information on setting storage
	 */
	public void setUserInfo(String username, String password, boolean autologin) {
		setAutoLogin(autologin);
		setUsername(username);
		setPassword(password);
	}
}
